/**
 * 
 */
package me.myachievements.web;

import org.apache.wicket.spring.injection.annot.SpringBean;

import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.injection.web.InjectorHolder;

import me.myachievements.persistence.GenericPersistenceManager;
import me.myachievements.persistence.PersistentObject;

/**
 * @author besseym
 *
 */
public class PersistentObjectModel<T extends PersistentObject> extends LoadableDetachableModel<T> {

	@SpringBean(name="genericPersistenceManager")
	GenericPersistenceManager genericPersistenceManager;

	private final Class<T> type;
	private final Long id;

	public PersistentObjectModel(Class<T> type, Long id) {
		InjectorHolder.getInjector().inject(this);
		this.type = type;
		this.id = id;
	}

	public PersistentObjectModel(T persistentObject) {
		super(persistentObject);
		InjectorHolder.getInjector().inject(this);
		this.type = (Class<T>) persistentObject.getClass();
		this.id = persistentObject.getId();
	}

	@Override
	protected T load() {
		return genericPersistenceManager.read(type, id);
	}

}
